<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
          <img src="/logo.png" class="logo" />
          <h2>学生用户画像系统</h2>
        </div>
      
      <!-- 角色切换选项卡 -->
      <el-tabs v-model="activeRole" class="login-tabs">
        <el-tab-pane label="学生登录" name="student">
          <el-form
            ref="studentFormRef"
            :model="studentForm"
            :rules="studentRules"
            label-position="top"
            class="login-form"
          >
            <el-form-item label="用户名" prop="username">
              <el-input
                v-model="studentForm.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="studentForm.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
              ></el-input>
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                class="login-button"
                @click="handleStudentLogin"
                :loading="loading"
              >
                学生登录
              </el-button>
              <div class="forgot-password-container">
                <el-link type="primary" :underline="false" @click="openForgotPasswordDialog('student')">
                  忘记密码？
                </el-link>
              </div>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="管理员登录" name="admin">
          <el-form
            ref="adminFormRef"
            :model="adminForm"
            :rules="adminRules"
            label-position="top"
            class="login-form"
          >
            <el-form-item label="用户名" prop="username">
              <el-input
                v-model="adminForm.username"
                placeholder="请输入管理员用户名"
                :prefix-icon="UserFilled"
                clearable
              ></el-input>
            </el-form-item>
            
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="adminForm.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
              ></el-input>
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                class="login-button"
                @click="handleAdminLogin"
                :loading="loading"
              >
                管理员登录
              </el-button>
              <div class="forgot-password-container">
                <el-link type="primary" :underline="false" @click="openForgotPasswordDialog('admin')">
                  忘记密码？
                </el-link>
              </div>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    
    <!-- 重置密码对话框 -->
    <el-dialog
      v-model="showForgotDialog"
      :title="resetUserType === 'student' ? '学生密码重置' : '管理员密码重置'"
      width="400px"
      center
    >
      <el-form
        ref="resetFormRef"
        :model="resetForm"
        :rules="resetRules"
        label-position="top"
        class="reset-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="resetForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          ></el-input>
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="resetForm.email"
            placeholder="请输入注册邮箱"
            :prefix-icon="Message"
            clearable
          ></el-input>
        </el-form-item>
        
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="resetForm.newPassword"
            type="password"
            placeholder="请输入新密码"
            :prefix-icon="Lock"
            show-password
          ></el-input>
        </el-form-item>
        
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            v-model="resetForm.confirmPassword"
            type="password"
            placeholder="请再次输入新密码"
            :prefix-icon="Lock"
            show-password
          ></el-input>
        </el-form-item>
      </el-form>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showForgotDialog = false">取消</el-button>
          <el-button type="primary" @click="handleResetPassword" :loading="resetLoading">
            重置密码
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { User, UserFilled, Lock, Message } from '@element-plus/icons-vue';

const emit = defineEmits(['login-success']);

const activeRole = ref('student'); // 默认选中学生登录
const loading = ref(false);
const resetLoading = ref(false);
const showForgotDialog = ref(false);
const resetUserType = ref('student'); // 当前重置密码的用户类型

// 学生登录表单
const studentFormRef = ref(null);
const studentForm = reactive({
  username: '',
  password: ''
});

const studentRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 20, message: '用户名长度在 4 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ]
};

// 管理员登录表单
const adminFormRef = ref(null);
const adminForm = reactive({
  username: '',
  password: ''
});

// 重置密码表单
const resetFormRef = ref(null);
const resetForm = reactive({
  username: '',
  email: '',
  newPassword: '',
  confirmPassword: ''
});

const resetRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 20, message: '用户名长度在 4 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== resetForm.newPassword) {
          callback(new Error('两次输入密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ]
};

const adminRules = {
  username: [
    { required: true, message: '请输入管理员用户名', trigger: 'blur' },
    { min: 4, max: 20, message: '用户名长度在 4 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ]
};

// 学生登录处理
const handleStudentLogin = async () => {
  try {
    await studentFormRef.value.validate();
    loading.value = true;
    
    // 模拟学生登录API调用
    setTimeout(() => {
      loading.value = false;
      
      // 模拟登录成功
      const userInfo = {
        username: studentForm.username,
        role: 'student',
        name: '学生用户'
      };
      
      // 存储用户信息到localStorage
      localStorage.setItem('userInfo', JSON.stringify(userInfo));
      
      ElMessage.success('学生登录成功');
      emit('login-success', userInfo);
    }, 1000);
  } catch (error) {
    console.error('学生登录验证失败:', error);
  }
};

// 管理员登录处理
const handleAdminLogin = async () => {
  try {
    await adminFormRef.value.validate();
    loading.value = true;
    
    // 模拟管理员登录API调用
    setTimeout(() => {
      loading.value = false;
      
      // 模拟登录成功
      const userInfo = {
        username: adminForm.username,
        role: 'admin',
        name: '管理员'
      };
      
      // 存储用户信息到localStorage
      localStorage.setItem('userInfo', JSON.stringify(userInfo));
      
      ElMessage.success('管理员登录成功');
      emit('login-success', userInfo);
    }, 1000);
  } catch (error) {
    console.error('管理员登录验证失败:', error);
  }
};

// 打开忘记密码对话框
const openForgotPasswordDialog = (userType) => {
  resetUserType.value = userType;
  // 清空重置密码表单
  resetForm.username = '';
  resetForm.email = '';
  resetForm.newPassword = '';
  resetForm.confirmPassword = '';
  if (resetFormRef.value) {
    resetFormRef.value.resetFields();
  }
  showForgotDialog.value = true;
};

// 处理重置密码
const handleResetPassword = async () => {
  try {
    await resetFormRef.value.validate();
    resetLoading.value = true;
    
    // 模拟重置密码API调用
    setTimeout(() => {
      resetLoading.value = false;
      showForgotDialog.value = false;
      
      ElMessage.success(
        resetUserType.value === 'student' ? '学生密码重置成功' : '管理员密码重置成功'
      );
    }, 1500);
  } catch (error) {
    console.error('重置密码验证失败:', error);
  }
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #e0e7ff 0%, #f0f9ff 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
}

.login-form {
  padding: 0 20px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.forgot-password-container {
  text-align: right;
  margin-top: 8px;
}

.reset-form {
  padding: 0 10px;
}

.dialog-footer {
  text-align: center;
}
</style>